@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM 项目集进度管理系统 Docker 启动脚本 (Windows)
REM 用于快速启动整个系统的 Docker 容器

set "action=%~1"
set "profile=%~2"

if "%action%"=="" set "action=start"

REM 颜色定义 (Windows 10/11)
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"

REM 日志函数
:log_info
echo %BLUE%[INFO]%NC% %~1
goto :eof

:log_success
echo %GREEN%[SUCCESS]%NC% %~1
goto :eof

:log_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof

:log_error
echo %RED%[ERROR]%NC% %~1
goto :eof

REM 检查 Docker 环境
:check_docker
call :log_info "检查 Docker 环境..."

docker --version >nul 2>&1
if errorlevel 1 (
    call :log_error "Docker 未安装，请先安装 Docker Desktop"
    pause
    exit /b 1
)

docker-compose --version >nul 2>&1
if errorlevel 1 (
    docker compose version >nul 2>&1
    if errorlevel 1 (
        call :log_error "Docker Compose 未安装，请先安装 Docker Compose"
        pause
        exit /b 1
    )
)

docker info >nul 2>&1
if errorlevel 1 (
    call :log_error "Docker 服务未启动，请启动 Docker Desktop"
    pause
    exit /b 1
)

call :log_success "Docker 环境检查通过"
goto :eof

REM 检查环境变量文件
:check_env_file
call :log_info "检查环境变量配置..."

if not exist ".env" (
    if exist ".env.docker" (
        call :log_warning "未找到 .env 文件，使用 .env.docker 作为模板"
        copy ".env.docker" ".env" >nul
        call :log_info "已创建 .env 文件，请根据需要修改配置"
    ) else (
        call :log_error "未找到环境变量配置文件，请创建 .env 文件"
        pause
        exit /b 1
    )
)

call :log_success "环境变量配置检查通过"
goto :eof

REM 创建必要的目录
:create_directories
call :log_info "创建必要的目录..."

set directories=logs\mysql logs\redis logs\backend logs\frontend logs\nginx uploads backups docker\nginx\ssl

for %%d in (%directories%) do (
    if not exist "%%d" (
        mkdir "%%d" 2>nul
        call :log_info "创建目录: %%d"
    )
)

call :log_success "目录创建完成"
goto :eof

REM 构建镜像
:build_images
call :log_info "构建 Docker 镜像..."

docker-compose --version >nul 2>&1
if errorlevel 1 (
    docker compose build --no-cache
) else (
    docker-compose build --no-cache
)

if errorlevel 1 (
    call :log_error "镜像构建失败"
    pause
    exit /b 1
)

call :log_success "镜像构建完成"
goto :eof

REM 启动服务
:start_services
call :log_info "启动服务..."

if not "%profile%"=="" (
    call :log_info "使用配置文件: %profile%"
    docker-compose --version >nul 2>&1
    if errorlevel 1 (
        docker compose --profile "%profile%" up -d
    ) else (
        docker-compose --profile "%profile%" up -d
    )
) else (
    docker-compose --version >nul 2>&1
    if errorlevel 1 (
        docker compose up -d
    ) else (
        docker-compose up -d
    )
)

if errorlevel 1 (
    call :log_error "服务启动失败"
    pause
    exit /b 1
)

call :log_success "服务启动完成"
goto :eof

REM 等待服务就绪
:wait_for_services
call :log_info "等待服务就绪..."

REM 等待数据库就绪
call :log_info "等待 MySQL 数据库启动..."
set /a timeout=60
:wait_mysql
if %timeout% leq 0 (
    call :log_error "MySQL 数据库启动超时"
    goto :wait_redis
)
docker exec project_management_mysql mysqladmin ping -h localhost --silent >nul 2>&1
if not errorlevel 1 (
    call :log_success "MySQL 数据库已就绪"
    goto :wait_redis
)
timeout /t 2 /nobreak >nul
set /a timeout-=2
goto :wait_mysql

:wait_redis
REM 等待 Redis 就绪
call :log_info "等待 Redis 缓存启动..."
set /a timeout=30
:wait_redis_loop
if %timeout% leq 0 (
    call :log_error "Redis 缓存启动超时"
    goto :wait_backend
)
docker exec project_management_redis redis-cli ping 2>nul | findstr "PONG" >nul
if not errorlevel 1 (
    call :log_success "Redis 缓存已就绪"
    goto :wait_backend
)
timeout /t 2 /nobreak >nul
set /a timeout-=2
goto :wait_redis_loop

:wait_backend
REM 等待后端服务就绪
call :log_info "等待后端服务启动..."
set /a timeout=60
:wait_backend_loop
if %timeout% leq 0 (
    call :log_warning "后端服务启动超时，请检查日志"
    goto :wait_frontend
)
curl -f http://localhost:8000/health >nul 2>&1
if not errorlevel 1 (
    call :log_success "后端服务已就绪"
    goto :wait_frontend
)
timeout /t 3 /nobreak >nul
set /a timeout-=3
goto :wait_backend_loop

:wait_frontend
REM 等待前端服务就绪
call :log_info "等待前端服务启动..."
set /a timeout=60
:wait_frontend_loop
if %timeout% leq 0 (
    call :log_warning "前端服务启动超时，请检查日志"
    goto :eof
)
curl -f http://localhost:8080 >nul 2>&1
if not errorlevel 1 (
    call :log_success "前端服务已就绪"
    goto :eof
)
timeout /t 3 /nobreak >nul
set /a timeout-=3
goto :wait_frontend_loop

REM 显示服务状态
:show_status
call :log_info "服务状态:"

docker-compose --version >nul 2>&1
if errorlevel 1 (
    docker compose ps
) else (
    docker-compose ps
)
goto :eof

REM 显示访问信息
:show_access_info
echo.
call :log_success "=== 项目集进度管理系统启动完成 ==="
echo.
echo 🌐 前端访问地址:
echo    http://localhost:8080
echo.
echo 🔧 后端 API 地址:
echo    http://localhost:8000
echo    API 文档: http://localhost:8000/docs
echo.
echo 🗄️  数据库管理工具:
echo    phpMyAdmin: http://localhost:8081
echo    Adminer: http://localhost:8082
echo.
echo 📊 Redis 管理工具:
echo    Redis Commander: http://localhost:8083
echo.
echo 📋 默认登录信息:
echo    用户名: admin
echo    密码: admin123
echo.
echo 📝 查看日志命令:
docker-compose --version >nul 2>&1
if errorlevel 1 (
    echo    docker compose logs -f [service_name]
) else (
    echo    docker-compose logs -f [service_name]
)
echo.
echo 🛑 停止服务命令:
docker-compose --version >nul 2>&1
if errorlevel 1 (
    echo    docker compose down
) else (
    echo    docker-compose down
)
echo.
goto :eof

REM 主程序
:main
if "%action%"=="start" (
    call :log_info "启动项目集进度管理系统..."
    call :check_docker
    if errorlevel 1 exit /b 1
    call :check_env_file
    if errorlevel 1 exit /b 1
    call :create_directories
    call :build_images
    if errorlevel 1 exit /b 1
    call :start_services
    if errorlevel 1 exit /b 1
    call :wait_for_services
    call :show_status
    call :show_access_info
) else if "%action%"=="stop" (
    call :log_info "停止项目集进度管理系统..."
    docker-compose --version >nul 2>&1
    if errorlevel 1 (
        docker compose down
    ) else (
        docker-compose down
    )
    call :log_success "系统已停止"
) else if "%action%"=="restart" (
    call :log_info "重启项目集进度管理系统..."
    call "%~f0" stop
    timeout /t 3 /nobreak >nul
    call "%~f0" start "%profile%"
) else if "%action%"=="status" (
    call :show_status
) else if "%action%"=="logs" (
    if not "%profile%"=="" (
        docker-compose --version >nul 2>&1
        if errorlevel 1 (
            docker compose logs -f "%profile%"
        ) else (
            docker-compose logs -f "%profile%"
        )
    ) else (
        docker-compose --version >nul 2>&1
        if errorlevel 1 (
            docker compose logs -f
        ) else (
            docker-compose logs -f
        )
    )
) else if "%action%"=="build" (
    call :log_info "重新构建镜像..."
    call :check_docker
    if errorlevel 1 exit /b 1
    call :build_images
) else if "%action%"=="clean" (
    call :log_info "清理 Docker 资源..."
    docker-compose --version >nul 2>&1
    if errorlevel 1 (
        docker compose down -v --rmi all
    ) else (
        docker-compose down -v --rmi all
    )
    docker system prune -f
    call :log_success "清理完成"
) else if "%action%"=="help" (
    echo 用法: %~nx0 [命令] [选项]
    echo.
    echo 命令:
    echo   start [profile]  启动系统 ^(默认^)
    echo   stop            停止系统
    echo   restart [profile] 重启系统
    echo   status          查看服务状态
    echo   logs [service]  查看日志
    echo   build           重新构建镜像
    echo   clean           清理 Docker 资源
    echo   help            显示帮助信息
    echo.
    echo 配置文件 ^(profile^):
    echo   tools           启动管理工具 ^(phpMyAdmin, Adminer, Redis Commander^)
    echo.
    echo 示例:
    echo   %~nx0 start        # 启动基本服务
    echo   %~nx0 start tools  # 启动所有服务包括管理工具
    echo   %~nx0 logs backend # 查看后端服务日志
    echo   %~nx0 stop         # 停止所有服务
) else (
    call :log_error "未知命令: %action%"
    echo 使用 '%~nx0 help' 查看帮助信息
    pause
    exit /b 1
)

goto :eof

REM 执行主程序
call :main

if "%action%"=="start" (
    echo.
    echo 按任意键退出...
    pause >nul
)